package com.zwl.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zwl.dao.BaseDao;
import com.zwl.dao.RoleDao;
import com.zwl.po.PageBean;
import com.zwl.po.RolePo;

public class RoleDaoImpl extends BaseDao<RolePo> implements RoleDao {

	@Override
	public List<RolePo> findByCondition(Map<String, Object> map) {
		List<Object> paramList=new ArrayList<>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from b_role where 1=1 ");
		if(map!=null && map.size()>0){
			if(map.get("isActive")!=null){
				sf.append(" and is_active=?");
				paramList.add(map.get("isActive"));
			}
			
			if(map.get("exincludeUserId")!=null){
				sf.append(" and id not in ( ");
				sf.append("   select role_id from b_user_role where user_id=?  ");
				sf.append(" )");
				paramList.add(map.get("exincludeUserId"));
			}
			
			if(map.get("includeUserId")!=null){
				sf.append(" and id in ( ");
				sf.append("   select role_id from b_user_role where user_id=?  ");
				sf.append(" )");
				paramList.add(map.get("includeUserId"));
			}
		}
		
		return this.executeQuery3(sf.toString(), null, RolePo.class, paramList.toArray());
	}

	@Override
	public RolePo findbyId(Integer id) {
		List<Object> paramList=new ArrayList<>();
		String sql=" select * from b_role where id=? ";
		paramList.add(id);
		
		List<RolePo> list=this.executeQuery3(sql, null, RolePo.class, paramList.toArray());
		return (list!=null && list.size()>0)?list.get(0):null;
	}



	@Override
	public RolePo getEntity(ResultSet rs) {
		
		return null;
	}

	@Override
	public int saveRoleMenu(Integer roleId, Integer menuId) {
		String sql="insert into b_role_menu values(?,?) ";
		List<Object> paramList=new ArrayList<>();
		paramList.add(roleId);
		paramList.add(menuId);
		return (int) this.executeUpdate(sql, null, null, paramList.toArray());
	}

	@Override
	public int deleteRoleMenu(Integer roleId, Integer menuId) {
		String sql="delete b_role_menu where role_id=? ";
		List<Object> paramList=new ArrayList<>();
		paramList.add(roleId);
		return (int) this.executeUpdate(sql, null, null, paramList.toArray());
	}

	@Override
	public int updateRole(RolePo entity) {
		StringBuffer sf=new StringBuffer();
		sf.append(" update b_role set ");
		sf.append("    name=? ");
		sf.append("   ,remark=? ");
		sf.append("   ,created_Id=? ");
		sf.append("   ,created_Time=? ");
		sf.append("   ,updated_Id=? ");
		sf.append("   ,updated_Time=? ");
		sf.append("   ,is_active=? ");
		sf.append(" where id=? ");
		List<Object> paramList=new ArrayList<>();
		paramList.add(entity.getName());
		paramList.add(entity.getRemark());
		paramList.add(entity.getCreatedId());
		paramList.add(entity.getCreatedTime());
		paramList.add(entity.getUpdatedId());
		paramList.add(entity.getUpdatedTime());
		paramList.add(entity.getIsActive());
		paramList.add(entity.getId());
		return (int) this.executeUpdate(sf.toString(), "update", null, paramList.toArray());
	}

	@Override
	public PageBean<RolePo> findPageByCondition(int pageIndex, int pageSize, Map<String, Object> map) {
		PageBean<RolePo> pagebean=new PageBean<RolePo>();
		pagebean.setPageIndex(pageIndex);
		pagebean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		
		StringBuffer baseSql=new StringBuffer();
		baseSql.append(" select * from b_role where 1=1 ");
		pagebean.setTotalCount(this.getCount(baseSql.toString(), paramList.toArray()));

		if(pagebean.getTotalCount()>0){
			StringBuffer pageSql=new StringBuffer();
			pageSql.append(" select * from ( ");
			pageSql.append("  select tmp1.*,rownum rn from ( ");
			pageSql.append(baseSql.toString());
			pageSql.append("  ) tmp1");
			pageSql.append(" ) tmp2");
			pageSql.append(" where tmp2.rn>? and tmp2.rn<=? ");
			int startRow=(pagebean.getPageIndex()-1)*pagebean.getPageSize();
			int endRow=pagebean.getPageIndex()*pagebean.getPageSize();
			paramList.add(startRow);//开始行号
			paramList.add(endRow);//结束行号
			
			Map<String, String> columnsMap=new HashMap<String, String>();
			columnsMap.put("role_id", "id");//列名与属性名不相同时
			pagebean.setList(this.executeQuery3(pageSql.toString(), columnsMap, RolePo.class, paramList.toArray()));
		}
		
		return pagebean;
	}

}
